package com.zbs.framework.dao.utils.jpa;

import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

public class SortUtil<T> {
    private final List<Sort.Order> orderList = new ArrayList<>();

    public Sort orderByDesc(String ...columns){
        if (columns.length > 0){
            for (String column : columns) {
                orderList.add(Sort.Order.desc(column));
            }
        }
        return Sort.by(orderList);
    }

    @SafeVarargs
    public final Sort orderByDesc(SFunction<T, ?>... columns){
        if (columns.length > 0){
            for (SFunction<T, ?> column : columns) {
                orderList.add(Sort.Order.desc(ColumnUtil.getName(column)));
            }
        }
        return Sort.by(orderList);
    }

    public Sort orderByAsc(String ...columns){
        if (columns.length > 0){
            for (String column : columns) {
                orderList.add(Sort.Order.asc(column));
            }
        }
        return Sort.by(orderList);
    }

    @SafeVarargs
    public final Sort orderByAsc(SFunction<T, ?>... columns){
        if (columns.length > 0){
            for (SFunction<T, ?> column : columns) {
                orderList.add(Sort.Order.asc(ColumnUtil.getName(column)));
            }
        }
        return Sort.by(orderList);
    }
}
